package com.snap.vseries.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.orhanobut.logger.Logger;
import com.snap.vseries.AppConfig;
import com.snap.vseries.Constants;
import com.snap.vseries.bean.SerialPortDevice;
import com.snap.vseries.serial.SerialPortHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PcrService extends Service {
    private SerialPortHelper serialPortHelper = new SerialPortHelper(new SerialPortDevice(AppConfig.SERIAL_PORT, AppConfig.SERIAL_BAUD_RATE));
    private PcrBinder mBinder = new PcrBinder();
    private HashMap<String, ExpController> hashMap = new HashMap<>();
    private List<String> controllerList;
    private String currentKey;

    @Override
    public void onCreate() {
        controllerList = new ArrayList<>();
        for (int i =0; i <AppConfig.DEVICES.length; i++) {
            hashMap.put("设备"+AppConfig.DEVICES[i], new ExpController(serialPortHelper,i, new IFinishedListener() {
                @Override
                public void onFinished() {
                    checkAllFinish();
                }
            }));
            controllerList.add("设备"+AppConfig.DEVICES[i]);
        }
        currentKey = "设备"+AppConfig.DEVICES[0];
    }
    public  void checkAllFinish(){
        for (String deviceId : controllerList) {
            ExpController expController=hashMap.get(deviceId);
            if (expController.getExpState()!= Constants.EXP_STATE_IDLE){
                return;
            }
        }
        Logger.e("没有试验在运行，大复位！");
        serialPortHelper.close();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public String getCurrentKey() {
        return currentKey;
    }

    public void setCurrentKey(String currentKey) {
        this.currentKey = currentKey;
    }

    public ExpController getCurrentController() {
        return hashMap.get(currentKey);
    }


    public List<String> getControllerList() {
        return controllerList;
    }

    public void setControllerList(List<String> controllerList) {
        this.controllerList = controllerList;
    }

    public void registerOnExpStateChangedListener(OnExpStateChangedListener stateListener) {
        for (String s : controllerList) {
            ExpController expController=hashMap.get(s);
            expController.registerOnExpStateChangedListener(stateListener);
        }
    }
    public void unregisterOnExpStateChangedListener(OnExpStateChangedListener listener) {
        for (String s : controllerList) {
            ExpController expController=hashMap.get(s);
            expController.unregisterOnExpStateChangedListener(listener);
        }
    }
    public class PcrBinder extends Binder {

        public PcrService getService() {
            return PcrService.this;
        }
    }
    public  interface IFinishedListener{
        public void onFinished();
    }
}
